package com.estore.network;

import com.estore.model.Product;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class EStoreClient {
    private static final String SERVER_HOST = "localhost";
    private static final int SERVER_PORT = 8888;
    private final Map<String, Integer> shoppingCart = new HashMap<>();
    private final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        new EStoreClient().start();
    }

    public void start() {
        while (true) {
            showMenu();
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    browseProducts();
                    break;
                case 2:
                    addToCart();
                    break;
                case 3:
                    viewCart();
                    break;
                case 4:
                    System.out.println("感谢使用，再见！");
                    return;
                case 99:
                    System.out.println("C/S架构下，备份功能已迁移至服务器，客户端无此权限！");
                    break;
                default:
                    System.out.println("无效的选项，请重新输入！");
            }
        }
    }

    private void showMenu() {
        System.out.println("\n ====欢迎来到商城=== ");
        System.out.println("1. 浏览所有商品");
        System.out.println("2. 添加商品到购物车");
        System.out.println("3. 查看购物车");
        System.out.println("4. 退出");
        System.out.print("请输入您的选择: ");
    }

    private void browseProducts() {
        System.out.println("\n--- 浏览所有商品 ---");
        try (Socket socket = new Socket(SERVER_HOST, SERVER_PORT);
             ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {

            oos.writeObject("GET_ALL_PRODUCTS");
            oos.flush();
            System.out.println("已向服务器发送商品列表请求...");

            Object response = ois.readObject();
            if (response instanceof List<?>) {
                List<Product> productList = (List<Product>) response;

                if (productList.isEmpty()) {
                    System.out.println("暂无商品可供浏览！");
                    return;
                }
                System.out.println("商品列表：");
                for (Product product : productList) {
                    System.out.printf("商品ID：%s，名称：%s，价格：%.2f元%n",
                            product.getId(), product.getName(), product.getPrice());
                }
            } else {
                System.out.println("服务器响应异常：" + response);
            }

        } catch (IOException | ClassNotFoundException e) {
            System.err.println("获取商品列表失败：" + e.getMessage());
            System.out.println("请检查服务器是否已启动，或网络连接是否正常！");
        }
    }

    private void addToCart() {
        System.out.println("\n--- 添加商品到购物车 ---");
        System.out.print("请输入商品ID：");
        String productId = scanner.nextLine();
        System.out.print("请输入购买数量：");
        int quantity = scanner.nextInt();
        scanner.nextLine();

        shoppingCart.put(productId, shoppingCart.getOrDefault(productId, 0) + quantity);
        System.out.println("商品已成功添加到购物车！");
    }

    private void viewCart() {
        System.out.println("\n--- 您的购物车 ---");
        if (shoppingCart.isEmpty()) {
            System.out.println("购物车为空，请先添加商品！");
            return;
        }
        for (Map.Entry<String, Integer> entry : shoppingCart.entrySet()) {
            System.out.printf("商品ID：%s，数量：%d%n", entry.getKey(), entry.getValue());
        }
    }
}